
package com.yanboo.modules.system.domain;

import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import com.yanboo.common.base.BaseEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

import static jakarta.persistence.ConstraintMode.NO_CONSTRAINT;

@Entity
@Getter
@Setter
@Schema(description = "部门")
@Table(name="sys_dept")
public class Dept extends BaseEntity implements Serializable {

    @JSONField(serialize = false)
    @ManyToMany(mappedBy = "depts",cascade = CascadeType.PERSIST)
    @org.hibernate.annotations.ForeignKey(name = "none")
    @Schema(description = "角色")
    private Set<Role> roles;

    @Schema(description = "排序")
    private Integer deptSort;

    @NotBlank
    @Schema(description = "部门名称")
    private String name;

    @NotNull
    @Schema(description = "是否启用")
    private Boolean enabled;

    @Schema(description = "上级部门")
    @Column(length = 20)
    private Long pid;

    @Schema(description = "子节点数目", hidden = true)
    @Column(length = 5)
    private Integer subCount = 0;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Dept dept = (Dept) o;
        return Objects.equals(getId(), dept.getId()) &&
                Objects.equals(name, dept.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), name);
    }
}